/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import dao.CuaHangDAO;
import dao.PhieuNhapHangDAO;
import java.awt.Component;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import org.apache.log4j.Logger;
import pojo.CuaHang;
import pojo.PhieuNhapHang;

/**
 *
 * @author 0
 */
public class DanhSachPhieuNhapHangGUI extends javax.swing.JPanel {

    /**
     * Creates new form DanhSachPhieuNhapHang
     */
    private List<PhieuNhapHang> phieuNhapHangs;
    private static Logger logger = Logger.getLogger(DanhSachPhieuNhapHangGUI.class);

    public DanhSachPhieuNhapHangGUI() throws Exception {
        initComponents();
        FormatControl();
        AutoLoad();
    }

    private void FormatControl() {
        Utility.FormatNumberStringTextfield(txtMaPhieuNhapHang);
    }

    private void AutoLoad() throws Exception {
        List<CuaHang> cuaHangs = (List<CuaHang>) CuaHangDAO.LayDanhSach(CuaHang.class, MainGUI.nguoiDung);
        Collections.sort(cuaHangs, new Comparator<CuaHang>() {
            @Override
            public int compare(CuaHang o1, CuaHang o2) {
                return o1.getTenCuaHang().compareTo(o2.getTenCuaHang());
            }
        });
        Utility.SetDataSourceInCombo(cuaHangs, cboCuaHang);
        cboCuaHang.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof CuaHang) {
                    CuaHang cuaHang = (CuaHang) value;
                    setText(cuaHang.getTenCuaHang());
                }
                return this;
            }
        });
        cboCuaHang.setSelectedIndex(-1);

        phieuNhapHangs = (List<PhieuNhapHang>) PhieuNhapHangDAO.LayDanhSach(PhieuNhapHang.class, MainGUI.nguoiDung);
        Utility.CreateColumnsFromDB(PhieuNhapHang.class, phieuNhapHangs, tbDanhSachPhieuNhapHang, new String[]{"Mã phiếu nhập hàng", "Ngày lập phiếu", "Tổng tiền nhập", "Cửa hàng"}, null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtMaPhieuNhapHang = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        dtNgayLapPhieuFrom = new org.jdesktop.swingx.JXDatePicker();
        jLabel3 = new javax.swing.JLabel();
        dtNgayLapPhieuTo = new org.jdesktop.swingx.JXDatePicker();
        jLabel4 = new javax.swing.JLabel();
        cboCuaHang = new javax.swing.JComboBox();
        btnTimKiem = new javax.swing.JButton();
        btnLapPhieuNhapHang = new javax.swing.JButton();
        btnXemChiTiet = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSachPhieuNhapHang = new org.jdesktop.swingx.JXTable();

        jLabel1.setText("Mã phiếu nhập hàng");

        jLabel2.setText("Ngày lập phiếu từ");

        jLabel3.setText("đến");

        jLabel4.setText("Cửa hàng");

        btnTimKiem.setText("Tìm kiếm");
        btnTimKiem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimKiemActionPerformed(evt);
            }
        });

        btnLapPhieuNhapHang.setText("Lập phiếu nhập hàng");
        btnLapPhieuNhapHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLapPhieuNhapHangActionPerformed(evt);
            }
        });

        btnXemChiTiet.setText("Xem chi tiết");
        btnXemChiTiet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXemChiTietActionPerformed(evt);
            }
        });

        btnClear.setText("Clear");
        btnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearActionPerformed(evt);
            }
        });

        tbDanhSachPhieuNhapHang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbDanhSachPhieuNhapHang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDanhSachPhieuNhapHangMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbDanhSachPhieuNhapHang);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(dtNgayLapPhieuFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(dtNgayLapPhieuTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtMaPhieuNhapHang)
                            .addComponent(cboCuaHang, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(83, 83, 83)
                        .addComponent(btnTimKiem)
                        .addGap(18, 18, 18)
                        .addComponent(btnLapPhieuNhapHang)
                        .addGap(18, 18, 18)
                        .addComponent(btnXemChiTiet)
                        .addGap(18, 18, 18)
                        .addComponent(btnClear)))
                .addContainerGap(118, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtMaPhieuNhapHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(dtNgayLapPhieuFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(dtNgayLapPhieuTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cboCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnTimKiem)
                    .addComponent(btnLapPhieuNhapHang)
                    .addComponent(btnXemChiTiet)
                    .addComponent(btnClear))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearActionPerformed
        // TODO add your handling code here:
        Utility.ClearControlInPanel(this);
    }//GEN-LAST:event_btnClearActionPerformed

    private void btnTimKiemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimKiemActionPerformed
        try {
            // TODO add your handling code here:
            Integer maPhieuNhapHang = txtMaPhieuNhapHang.getText().isEmpty() ? null : Integer.parseInt(txtMaPhieuNhapHang.getText());
            Integer idCuaHang = null;
            CuaHang cuaHang = (CuaHang) cboCuaHang.getModel().getSelectedItem();
            if (cuaHang != null) {
                idCuaHang = cuaHang.getId();
            }
            phieuNhapHangs = PhieuNhapHangDAO.TimKiem(maPhieuNhapHang, dtNgayLapPhieuFrom.getDate(), dtNgayLapPhieuTo.getDate(), idCuaHang, MainGUI.nguoiDung);
            Utility.CreateColumnsFromDB(PhieuNhapHang.class, phieuNhapHangs, tbDanhSachPhieuNhapHang, new String[]{"Mã phiếu nhập hàng", "Ngày lập phiếu", "Tổng tiền nhập", "Cửa hàng"}, null);
            if (phieuNhapHangs.isEmpty()) {
                JOptionPane.showMessageDialog(this, "Không tìm thấy");
            }
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnTimKiemActionPerformed

    private void btnLapPhieuNhapHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLapPhieuNhapHangActionPerformed
        try {
            // TODO add your handling code here:
            PhieuNhapHangGUI phieuNhapHangGUI=new PhieuNhapHangGUI(null, true, new PhieuNhapHang());
            final int x = (((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this)).getWidth() - phieuNhapHangGUI.getWidth()) / 2;
            final int y = (((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this)).getHeight() - phieuNhapHangGUI.getHeight()) / 2;
            phieuNhapHangGUI.setLocation(x, y);
            phieuNhapHangGUI.setButtonLuuClickedListener(new MyEventListener() {

                @Override
                public void myEventOccured(Object evt) {
                    PhieuNhapHang phieuNhapHang=(PhieuNhapHang) evt;
                    if(phieuNhapHang!=null){
                        phieuNhapHangs.add(phieuNhapHang);
                        Utility.CreateColumnsFromDB(PhieuNhapHang.class, phieuNhapHangs, tbDanhSachPhieuNhapHang, new String[]{"Mã phiếu nhập hàng", "Ngày lập phiếu", "Tổng tiền nhập", "Cửa hàng"}, null);
                        tbDanhSachPhieuNhapHang.changeSelection(tbDanhSachPhieuNhapHang.getModel().getRowCount()-1, 0, false, false);
                    }
                }
            });
            phieuNhapHangGUI.setVisible(true);
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnLapPhieuNhapHangActionPerformed

    private void btnXemChiTietActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXemChiTietActionPerformed
        // TODO add your handling code here:
        List<PhieuNhapHang> pnhs=(List<PhieuNhapHang>) Utility.GetSelectedRows(tbDanhSachPhieuNhapHang, phieuNhapHangs);
        if(!pnhs.isEmpty()){
            try {
                int index=phieuNhapHangs.indexOf(pnhs.get(0));
                PhieuNhapHangGUI phieuNhapHangGUI=new PhieuNhapHangGUI(null, true, pnhs.get(0));
                final int x = (((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this)).getWidth() - phieuNhapHangGUI.getWidth()) / 2;
                final int y = (((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, this)).getHeight() - phieuNhapHangGUI.getHeight()) / 2;
                phieuNhapHangGUI.setLocation(x, y);
                phieuNhapHangGUI.setVisible(true);
                Utility.CreateColumnsFromDB(PhieuNhapHang.class, phieuNhapHangs, tbDanhSachPhieuNhapHang, new String[]{"Mã phiếu nhập hàng", "Ngày lập phiếu", "Tổng tiền nhập", "Cửa hàng"}, null);
                tbDanhSachPhieuNhapHang.changeSelection(index, 0, false, false);
            } catch (Exception ex) {
                logger.error(new Date().toString(), ex);
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }
        }
        else{
            JOptionPane.showMessageDialog(this, "Xin chọn dữ liệu trên lưới để xem chi tiết");
        }
    }//GEN-LAST:event_btnXemChiTietActionPerformed

    private void tbDanhSachPhieuNhapHangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDanhSachPhieuNhapHangMouseClicked
        // TODO add your handling code here:
        if(evt.getClickCount()==2){
            btnXemChiTietActionPerformed(null);
        }
    }//GEN-LAST:event_tbDanhSachPhieuNhapHangMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnLapPhieuNhapHang;
    private javax.swing.JButton btnTimKiem;
    private javax.swing.JButton btnXemChiTiet;
    private javax.swing.JComboBox cboCuaHang;
    private org.jdesktop.swingx.JXDatePicker dtNgayLapPhieuFrom;
    private org.jdesktop.swingx.JXDatePicker dtNgayLapPhieuTo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTable tbDanhSachPhieuNhapHang;
    private javax.swing.JTextField txtMaPhieuNhapHang;
    // End of variables declaration//GEN-END:variables
}
